package com.ailink.basic.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {
    public static void main(String[] args) throws InterruptedException {
        LockSupport.unpark(Thread.currentThread());
        LockSupport.unpark(Thread.currentThread());
        LockSupport.park();
//        LockSupport.park();
//        LockSupport.park();

        ReentrantLock lock = new ReentrantLock();

        Condition condition = lock.newCondition();

        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread() + "lock.lock()");
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread() + "condition.await()");

            lock.unlock();
            System.out.println(Thread.currentThread() + "lock.unlock()");
        }, "condition_await").start();

        Thread.sleep(1000);
        lock.lock();
        new Thread(() -> {
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread 3 lock").start();
        System.out.println("condition.signal()");
        condition.signal();
        Thread.sleep(1000);
        System.out.println("lock.unlock()");
        lock.unlock();

    }
}
